/**
 * Created with IntelliJ IDEA.
 * User: Katerina
 * Date: 15.01.13
 * Time: 3:39
 * To change this template use File | Settings | File Templates.
 */
package ru.gameChangers.homework.businessModels {
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;


public class StatusLine extends Sprite {

    private var title:TextField = new TextField();
    private var description:TextField = new TextField();

    private var closeMode:Boolean = false;

    public function StatusLine(width:int) {

        title.defaultTextFormat = new TextFormat(ConfigurationConstants.FONT, 19, ConfigurationConstants.TITLE_TEXT_COLOR, true);
        title.y = 14;
        title.x = -width / 8 + 17;
        var descriptionTextFormant:TextFormat = new TextFormat(ConfigurationConstants.FONT, 14, ConfigurationConstants.CONTENT_TEXT_COLOR);
        descriptionTextFormant.align = TextFormatAlign.RIGHT;
        description.defaultTextFormat = descriptionTextFormant;
        description.y = 13 + 20 + 13;
        description.x = -width / 8 + 8;
        description.wordWrap = true;
        description.width = width + width / 4 - 16;
        description.height = ConfigurationConstants.STATUS_LINE_HEIGHT - description.y - 10;

        drawGraphics(width);
        addEventListener(Event.ENTER_FRAME, onEnterFrame);
        alpha = 0;
    }

    private function onEnterFrame(e:Event):void {
        if (closeMode && alpha > 0) {
            alpha -= 0.05;
        } else {
            closeMode = false;
        }
    }

    public function updateStatus(titleText:String, descriptionText:String):void {

        updateStatusTextField(title, titleText + ":");
        updateStatusTextField(description, descriptionText);
    }

    public function close() : void {
        closeMode = true;
    }

    private function updateStatusTextField(textField:TextField, text:String) : void {
        if (contains(textField)) {
            removeChild(textField);
        }
        textField.text = text;
        addChild(textField);
        closeMode = false;
        alpha = 1;
    }

    private function drawGraphics(width:int) : void {
        graphics.lineStyle(1, ConfigurationConstants.TITLE_TEXT_COLOR);
        graphics.moveTo(-width / 8, 12);
        graphics.lineTo(10, 12);
        graphics.lineTo(18, 0);
        graphics.lineTo(26, 12);
        graphics.lineTo(width + width / 8, 12);
        graphics.moveTo(-width / 8, 12 + ConfigurationConstants.STATUS_LINE_HEIGHT);
        graphics.lineTo(width + width / 8, 12 + ConfigurationConstants.STATUS_LINE_HEIGHT);
        graphics.lineStyle(1, ConfigurationConstants.DARK_LINE_COLOR);
        graphics.moveTo(description.x, 12 + 24);
        graphics.lineTo(width / 5, 12 + 24);

    }
}
}
